package com.stek.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

import com.stek.R;
import com.stek.model.AppInfo;
import com.stek.model.FloatInfo;
import com.stek.model.Wifi;
import com.stek.utils.Utils;

public class DBHelper extends SQLiteOpenHelper {
	// ==================================================================================
	private static final String DB_NAME = "DBHelper";
	private static final int DB_VERSION = 1;
	private static DBHelper mDbHelper;
	private SQLiteDatabase mDB;
	private Context mContext;

	// ==================================================================================
	public static DBHelper getInstance(Context context) {
		synchronized (DBHelper.class) {
			if (mDbHelper == null) {
				mDbHelper = new DBHelper(context);
			}
		}
		return mDbHelper;
	}

	public DBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		mContext = context;
		mDB = getWritableDatabase();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(FAVORITE.CREATE_TABLE);
		db.execSQL(MAIN.CREATE_TABLE);
		db.execSQL(SETTINGS.CREATE_TABLE);
		db.execSQL(WIFI.CREATE_TABLE);
		initMain(mContext, db);
		initSettings(mContext, db);
		initFav(mContext, db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	// ==================================================================================

	public void deleteTable(String table) {
		this.mDB.delete(table, null, null);
	}

	private void initMain(Context context, SQLiteDatabase db) {
		insertMain(
				db,
				context,
				new FloatInfo(context.getResources().getString(
						R.string.tag_lock), R.drawable.btn_lock, 1));
		insertMain(
				db,
				context,
				new FloatInfo(context.getResources().getString(
						R.string.tag_recent), R.drawable.btn_recent, 3));
		insertMain(
				db,
				context,
				new FloatInfo(context.getResources().getString(
						R.string.tag_home), R.drawable.btn_home, 4));
		insertMain(db, context,
				new FloatInfo(context.getResources()
						.getString(R.string.tag_app), R.drawable.btn_app, 5));
		insertMain(db, context,
				new FloatInfo(context.getResources()
						.getString(R.string.tag_cpu), R.drawable.btn_cpu, 7));

	}

	private void initSettings(Context context, SQLiteDatabase db) {
		if (Utils.CameraHardware.hasTorch(context)) {
			insertSettings(db, context, new FloatInfo(context.getResources()
					.getString(R.string.tag_light), R.drawable.btn_light, 0));
		} else {
			insertSettings(db, context, new FloatInfo(context.getResources()
					.getString(R.string.tag_camera), R.drawable.btn_camera, 0));
		}
		if (Utils.CameraHardware.hasCameraFront(context)) {
			insertSettings(db, context, new FloatInfo(context.getResources()
					.getString(R.string.tag_mirror), R.drawable.btn_mirror, 1));
		} else {
			insertSettings(db, context, new FloatInfo(context.getResources()
					.getString(R.string.tag_camera), R.drawable.btn_camera, 1));
		}

		// insertSettings(
		// db,context,
		// new FloatInfo(context.getResources().getString(
		// R.string.tag_camera), R.drawable.btn_camera, 2));
		insertSettings(db, context, new FloatInfo(context.getResources()
				.getString(R.string.tag_sound), R.drawable.btn_volume_on, 2));
		insertSettings(db, context, new FloatInfo(context.getResources()
				.getString(R.string.tag_wifi), R.drawable.btn_wifi, 3));
		if (Utils.Telephony.hasTelephony(context)) {
			insertSettings(db, context, new FloatInfo(context.getResources()
					.getString(R.string.tag_data), R.drawable.btn_data, 4));
		}

		if (Utils.Bluetooth.hasBluetooth(context)) {
			insertSettings(db, context, new FloatInfo(context.getResources()
					.getString(R.string.tag_bluetooth),
					R.drawable.btn_bluetooth, 5));
		}

		insertSettings(db, context, new FloatInfo(context.getResources()
				.getString(R.string.tag_airplane), R.drawable.btn_airplane, 6));
		if (Utils.GPS.hasLocationGPS(context)) {
			insertSettings(db, context, new FloatInfo(context.getResources()
					.getString(R.string.tag_gps), R.drawable.btn_gps, 7));
		}

		insertSettings(db, context, new FloatInfo(context.getResources()
				.getString(R.string.tag_rotate), R.drawable.btn_rotate, 8));
		// insertSettings(
		// db,context,
		// new FloatInfo(context.getResources().getString(
		// R.string.tag_sound), R.drawable.btn_volume_on, 9));
		// insertSettings(
		// db,context,
		// new FloatInfo(context.getResources().getString(
		// R.string.tag_settings), R.drawable.btn_settings, 10));

	}

	private void initFav(Context context, SQLiteDatabase db) {
		insertFavorite(db, context, new AppInfo(context.getPackageName(),
				context.getString(R.string.app_name), 4));

	}

	// ==================================================================================
	public void insertMain(Context context, FloatInfo info) {
		ContentValues values = new ContentValues();
		values.put(MAIN.TAG, info.tag);
		values.put(MAIN.POSITION, info.position);
		values.put(MAIN.RESOURCE, info.resource);
		deleteMainWithPosition(context, info.position);
		mDB.insert(MAIN.TABLE, null, values);
	}

	public void insertMain(SQLiteDatabase db, Context context, FloatInfo info) {
		ContentValues values = new ContentValues();
		values.put(MAIN.TAG, info.tag);
		values.put(MAIN.POSITION, info.position);
		values.put(MAIN.RESOURCE, info.resource);
		deleteMainWithPosition(context, info.position);
		db.insert(MAIN.TABLE, null, values);
	}

	public void deleteMainWithId(Context context, int id) {
		try {
			mDB.delete(MAIN.TABLE, MAIN._ID + " =? ",
					new String[] { String.valueOf(id) });
		} catch (Exception e) {
		}

	}

	public void deleteMainWithPosition(Context context, int position) {
		try {
			mDB.delete(MAIN.TABLE, MAIN.POSITION + " =? ",
					new String[] { String.valueOf(position) });
		} catch (Exception e) {
		}

	}

	public FloatInfo findMain(Context context, int position) {
		Cursor cursor = mDB.query(MAIN.TABLE, MAIN.PROJECTION, MAIN.POSITION
				+ " =? ", new String[] { String.valueOf(position) }, null,
				null, null);
		if (cursor != null && cursor.moveToFirst()) {
			FloatInfo info = new FloatInfo();
			info.tag = cursor.getString(cursor.getColumnIndex(MAIN.TAG));
			info.position = cursor.getInt(cursor.getColumnIndex(MAIN.POSITION));
			info.resource = cursor.getInt(cursor.getColumnIndex(MAIN.RESOURCE));
			info.id = cursor.getInt(cursor.getColumnIndex(MAIN._ID));
			return info;
		}
		return null;
	}

	public void insertSettings(Context context, FloatInfo info) {
		ContentValues values = new ContentValues();
		values.put(SETTINGS.TAG, info.tag);
		values.put(SETTINGS.POSITION, info.position);
		values.put(SETTINGS.RESOURCE, info.resource);
		deleteSettingsWithPosition(context, info.position);
		mDB.insert(SETTINGS.TABLE, null, values);
	}

	public void insertSettings(SQLiteDatabase db, Context context,
			FloatInfo info) {
		ContentValues values = new ContentValues();
		values.put(SETTINGS.TAG, info.tag);
		values.put(SETTINGS.POSITION, info.position);
		values.put(SETTINGS.RESOURCE, info.resource);
		deleteSettingsWithPosition(context, info.position);
		db.insert(SETTINGS.TABLE, null, values);
	}

	public void deleteSettingsWithId(Context context, int id) {
		try {
			mDB.delete(SETTINGS.TABLE, SETTINGS._ID + " =? ",
					new String[] { String.valueOf(id) });
		} catch (Exception e) {
		}

	}

	public void deleteSettingsWithPosition(Context context, int position) {
		try {
			mDB.delete(SETTINGS.TABLE, SETTINGS.POSITION + " =? ",
					new String[] { String.valueOf(position) });
		} catch (Exception e) {
		}

	}

	public FloatInfo findSettings(Context context, int position) {
		Cursor cursor = mDB.query(SETTINGS.TABLE, SETTINGS.PROJECTION,
				SETTINGS.POSITION + " =? ",
				new String[] { String.valueOf(position) }, null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			FloatInfo info = new FloatInfo();
			info.tag = cursor.getString(cursor.getColumnIndex(SETTINGS.TAG));
			info.position = cursor.getInt(cursor
					.getColumnIndex(SETTINGS.POSITION));
			info.resource = cursor.getInt(cursor
					.getColumnIndex(SETTINGS.RESOURCE));
			info.id = cursor.getInt(cursor.getColumnIndex(SETTINGS._ID));
			return info;
		}
		return null;
	}

	public ArrayList<AppInfo> getAllFavoriteApps(Context context) {
		Cursor cursor = mDB.query(FAVORITE.TABLE, FAVORITE.PROJECTION, null,
				null, null, null, null);
		ArrayList<AppInfo> infos = new ArrayList<AppInfo>();
		if (cursor != null && cursor.moveToFirst()) {
			do {
				AppInfo info = new AppInfo(cursor.getString(1),
						cursor.getString(2), AppInfo.ACTION_LAUNCH_FAV_APP,
						cursor.getInt(3), cursor.getInt(0));
				infos.add(info);
			} while (cursor.moveToNext());
		}

		return infos;
	}

	public void insertFavorite(Context context, AppInfo info) {
		ContentValues values = new ContentValues();
		values.put(FAVORITE.PGK, info.pgk);
		values.put(FAVORITE.NAME, info.name);
		values.put(FAVORITE.POSITION, info.position);
		deleteFavorite(context, info.position);
		mDB.insert(FAVORITE.TABLE, null, values);
	}

	public void insertFavorite(SQLiteDatabase db, Context context, AppInfo info) {
		ContentValues values = new ContentValues();
		values.put(FAVORITE.PGK, info.pgk);
		values.put(FAVORITE.NAME, info.name);
		values.put(FAVORITE.POSITION, info.position);
		deleteFavorite(context, info.position);
		db.insert(FAVORITE.TABLE, null, values);
	}

	public void deleteFavorite(Context context, AppInfo info) {
		mDB.delete(FAVORITE.TABLE, FAVORITE._ID + " =? ",
				new String[] { String.valueOf(info.id) });
	}

	public void deleteFavorite(Context context, int position) {
		try {
			mDB.delete(FAVORITE.TABLE, FAVORITE.POSITION + " =? ",
					new String[] { String.valueOf(position) });
		} catch (Exception e) {
		}
	}

	public AppInfo findFavorite(Context context, int position) {
		Cursor cursor = mDB.query(FAVORITE.TABLE, FAVORITE.PROJECTION,
				FAVORITE.POSITION + " =? ",
				new String[] { String.valueOf(position) }, null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			AppInfo info = new AppInfo(cursor.getString(1),
					cursor.getString(2), AppInfo.ACTION_LAUNCH_FAV_APP,
					cursor.getInt(3), cursor.getInt(0));
			return info;
		}

		return null;
	}

	public ArrayList<Wifi> getTrustWifi(Context context) {
		Cursor cursor = mDB.query(WIFI.TABLE, WIFI.PROJECTION, null, null,
				null, null, null);
		ArrayList<Wifi> infos = new ArrayList<Wifi>();
		if (cursor != null && cursor.moveToFirst()) {
			do {
				Wifi wifiInfo = new Wifi();
				wifiInfo.SSID = cursor.getString(cursor
						.getColumnIndex(WIFI.SSID));
				wifiInfo.BSSID = cursor.getString(cursor
						.getColumnIndex(WIFI.BSSID));
				infos.add(wifiInfo);
			} while (cursor.moveToNext());
		}

		return infos;
	}

	public void insertTrustWifi(Context context, Wifi info) {
		ContentValues values = new ContentValues();
		values.put(WIFI.BSSID, info.BSSID);
		values.put(WIFI.SSID, info.SSID);
		mDB.insert(WIFI.TABLE, null, values);
	}

	public void deleteTrustWifi(Context context, Wifi info) {
		mDB.delete(WIFI.TABLE, WIFI.BSSID + " =? ",
				new String[] { String.valueOf(info.BSSID) });
	}

	// ==================================================================================
	private static class FAVORITE implements BaseColumns {
		private static final String TABLE = "_favorieTbl";
		private static final String PGK = "_pgk";
		private static final String NAME = "_name";
		private static final String POSITION = "_position";
		private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
				+ TABLE
				+ "("
				+ _ID
				+ " INTEGER PRIMARY KEY,"
				+ PGK
				+ " TEXT,"
				+ NAME + " TEXT," + POSITION + " INTEGER" + ")";

		private static final String[] PROJECTION = { _ID, PGK, NAME, POSITION };
	}

	private static class MAIN implements BaseColumns {
		private static final String TABLE = "_mainTbl";
		private static final String TAG = "_tag";
		private static final String POSITION = "_position";
		private static final String RESOURCE = "_resource";
		private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
				+ TABLE
				+ "("
				+ _ID
				+ " INTEGER PRIMARY KEY,"
				+ TAG
				+ " TEXT,"
				+ RESOURCE + " INTEGER," + POSITION + " INTEGER" + ")";

		private static final String[] PROJECTION = { _ID, TAG, POSITION,
				RESOURCE };
	}

	private static class SETTINGS implements BaseColumns {
		private static final String TABLE = "_settingsTbl";
		private static final String TAG = "_tag";
		private static final String POSITION = "_position";
		private static final String RESOURCE = "_resource";
		private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
				+ TABLE
				+ "("
				+ _ID
				+ " INTEGER PRIMARY KEY,"
				+ TAG
				+ " TEXT,"
				+ RESOURCE + " INTEGER," + POSITION + " INTEGER" + ")";

		private static final String[] PROJECTION = { _ID, TAG, POSITION,
				RESOURCE };
	}

	public static class WIFI implements BaseColumns {
		public static final String TABLE = "_wifiTbl";
		private static final String SSID = "_ssid";
		private static final String BSSID = "_bssid";
		private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
				+ TABLE
				+ "("
				+ _ID
				+ " INTEGER PRIMARY KEY,"
				+ SSID
				+ " TEXT, " + BSSID + " TEXT " + ")";

		private static final String[] PROJECTION = { _ID, SSID, BSSID };
	}
}
